/*
 * Copyright 2025 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.cloud.ai.manus.tool.knowledge;

import com.alibaba.cloud.ai.manus.tool.AbstractBaseTool;
import com.alibaba.cloud.ai.manus.tool.code.ToolExecuteResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.openai.api.OpenAiApi;

public class SearchCioTool extends AbstractBaseTool<SearchCioTool.SearchCioInput> {

    private static final Logger log = LoggerFactory.getLogger(SearchCioTool.class);
    
    private static String PARAMETERS = """
            {
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "(required) 用户涉及资产类咨询的问题."
                    }
                },
                "required": ["query"]
            }
            """;

    private static final String name = "search_cio";

    private static final String description = """
            根据用户问题获取资产策略信息.
            """;

    public static OpenAiApi.FunctionTool getToolDefinition() {
        OpenAiApi.FunctionTool.Function function = new OpenAiApi.FunctionTool.Function(description, name, PARAMETERS);
        OpenAiApi.FunctionTool functionTool = new OpenAiApi.FunctionTool(function);
        return functionTool;
    }
    
    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public String getParameters() {
        return PARAMETERS;
    }

    @Override
    public Class<SearchCioInput> getInputType() {
        return SearchCioInput.class;
    }

    @Override
    public ToolExecuteResult run(SearchCioInput input) {
        String query = input.getQuery();
        log.info("SearchCioTool query:{}", query);
        try {

            return new ToolExecuteResult("资产策略信息信息：通胀受益资产45–50%，通缩受益资产30%，桥梁型策略资产20–25%。");
        } catch (Exception e) {
            log.error("Error executing SearchCioTool search", e);
            return new ToolExecuteResult("Error executing SearchCioTool search: " + e.getMessage());
        }
    }

    @Override
    public String getCurrentToolStateString() {
        return String.format("""
				获取资产配置策略信息: %s
				""", "成功");
    }

    @Override
    public void cleanup(String planId) {
        // do nothing
    }

    @Override
    public String getServiceGroup() {
        return "default-service-group";
    }

    /**
     * Internal input class for defining input parameters of Google search tool
     */
    public static class SearchCioInput {

        private String query;

        public SearchCioInput(String query) {
            this.query = query;
        }

        public String getQuery() {
            return query;
        }

        public void setQuery(String query) {
            this.query = query;
        }

    }

}
